#include <gtk/gtk.h>
#include <glib.h>
#include <dbus/dbus-glib.h>

#define CMD_PERFIX "[LOG] "
#define PROGRAM_NAME "rhCtl"

void cb_prev(GtkWidget *widget, gpointer data)
{
	g_print("%s", CMD_PERFIX);
	g_print("Previous Song.\n");
	DBusGProxy *proxy = data;
	GError *error;

	g_type_init();
	error = NULL;

	if (!dbus_g_proxy_call(proxy,
			       	"previous",
			       	&error,
				G_TYPE_INVALID,
			       	G_TYPE_INVALID)) {
		if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION)
			g_printerr("Remote method exception %s: %s\n", dbus_g_error_get_name(error), error->message);
		g_error_free(error);
	}
}

void cb_pause(GtkWidget *widget, gpointer data)
{
	g_print("%s", CMD_PERFIX);
	g_print("Pause.\n");
	DBusGProxy *proxy = data;
	GError *error;

	g_type_init();
	error = NULL;

	if (!dbus_g_proxy_call(proxy,
				"playPause",
				&error,
				G_TYPE_BOOLEAN, TRUE,
			       	G_TYPE_INVALID,
			       	G_TYPE_INVALID)) {
		if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION)
			g_printerr("Remote method exception %s: %s\n", dbus_g_error_get_name(error), error->message);
		g_error_free(error);
	}
}

void cb_next(GtkWidget *widget, gpointer data)
{
	g_print("%s", CMD_PERFIX);
	g_print("Next Song.\n");
	DBusGProxy *proxy = data;
	GError *error;

	g_type_init();
	error = NULL;

	if (!dbus_g_proxy_call(proxy,
			       	"next",
			       	&error,
				G_TYPE_INVALID,
			       	G_TYPE_INVALID)) {
		if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION)
			g_printerr("Remote method exception %s: %s\n", dbus_g_error_get_name(error), error->message);
		g_error_free(error);
	}
}

gint delete_event(GtkWidget *widget, GdkEvent *event, gpointer data)
{
	return FALSE;
}

void destroy(GtkWidget *widget, gpointer data)
{
	g_print("%s", CMD_PERFIX);
	g_print("Quit!\n");
	gtk_main_quit();
}

int main(int argc, char *argv[])
{
	GError *error;
	DBusGConnection *conn;
	DBusGProxy *proxy;

	GtkWidget *window;
	GtkWidget *toolbar;
	GtkToolItem *tb_prev;
	GtkToolItem *tb_pause;
	GtkToolItem *tb_next;

	g_type_init();
	error = NULL;
	
	conn = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
	if (NULL == conn) {
		g_printerr("Connection Failed: %s.\n", error->message);
		g_error_free(error);
		return 1;
	}

	proxy = dbus_g_proxy_new_for_name(conn,
			"org.gnome.Rhythmbox",
			"/org/gnome/Rhythmbox/Player",
			"org.gnome.Rhythmbox.Player");
	error = NULL;

	gtk_init(&argc, &argv);
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(window), PROGRAM_NAME);
	gtk_widget_set_size_request(window, 140, 64);
	gtk_container_set_border_width(GTK_CONTAINER(window), 10); gtk_window_set_resizable(GTK_WINDOW(window), FALSE);

	g_signal_connect(G_OBJECT(window), "delete_event",
			G_CALLBACK(delete_event), NULL);
	g_signal_connect(G_OBJECT(window), "destroy",
			G_CALLBACK(destroy), NULL);

	toolbar = gtk_toolbar_new();
	gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);

	tb_prev = gtk_tool_button_new_from_stock(GTK_STOCK_MEDIA_PREVIOUS);
	g_signal_connect(G_OBJECT(tb_prev), "clicked",
			G_CALLBACK(cb_prev), proxy);
	gtk_toolbar_insert(GTK_TOOLBAR(toolbar), tb_prev, -1);

	tb_pause = gtk_tool_button_new_from_stock(GTK_STOCK_MEDIA_PAUSE);
	g_signal_connect(G_OBJECT(tb_pause), "clicked",
			G_CALLBACK(cb_pause), proxy);
	gtk_toolbar_insert(GTK_TOOLBAR(toolbar), tb_pause, -1);

	tb_next = gtk_tool_button_new_from_stock(GTK_STOCK_MEDIA_NEXT);
	g_signal_connect(G_OBJECT(tb_next), "clicked",
			G_CALLBACK(cb_next), proxy);
	gtk_toolbar_insert(GTK_TOOLBAR(toolbar), tb_next, -1);

	gtk_container_add(GTK_CONTAINER(window), toolbar);
	gtk_widget_show(GTK_WIDGET(tb_prev));
	gtk_widget_show(GTK_WIDGET(tb_pause));
	gtk_widget_show(GTK_WIDGET(tb_next));
	gtk_widget_show(toolbar);
	gtk_widget_show(window);


	gtk_main();

	if (NULL != error) g_error_free(error);
	g_object_unref(proxy);
	return 0;
}
